package net.lgpage.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Filter {

	interface CollectionFilter<M,T> {

		public boolean accpet(M t, T listt);

		public List<T> doFilter(M t, List<T> list);
		public List<T> doFilter(M t, Set<T> list);
	}
	/*
	 * 过滤集合中的对象 使之成为过滤后的集合
	 */
	public static  abstract class ListFilter<M,T> implements CollectionFilter<M,T>{
		
		@Override
		public List<T> doFilter(M t, List<T> list) {
			if(list==null)
				return null;
				else{
					List<T>slist = new ArrayList<T>();
					for(int i = 0;i<list.size();i++){
						T s = list.get(i);
						if(accpet(t, s)){
							slist.add(s);
						}
					}
					return slist;
				}
		}
		@Override
		public List<T> doFilter(M t,Set<T>list){
			if(list==null)
				return null;
				else{
					List<T>slist = new ArrayList<T>();
					Iterator<T>it = list.iterator();
					while(it.hasNext()){
						T s = it.next();
						if(accpet(t, s)){
							slist.add(s);
						}
					}
					return slist;
				}
		}
		
	}
}
